<?php
namespace App\Library\System;

class Config
{
    // MODE
    const mode = 0;
    const compress = false;

    // Server Configuration
    public $protocol;
    public $server;
    public $port;

    public $dir = 'public';

    // Cache Configurations
    const cache_client = true;
    const cache_server = true;

    // Cookie Configuration
    const cookie_timeout = 10; // days

    // Language Configurations
    const default_language = 'vi_VN';

    // Image Configuration
    const img_app_social = 'app/app/social';
    const img_app_avatar_original = 'user/avatar/original';
    const img_app_avatar_current = 'user/avatar/current';
    const img_app_flag = 'user/flag';

    private function __construct()
    {
        $this->initialize($_SESSION['config']);

        // ROOT SITE
        $this->root = realpath(ROOT . '/public');
        $this->protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        $this->server = $_SERVER['SERVER_NAME'];
        $this->port = ($_SERVER['SERVER_PORT'] != 80) ? $_SERVER['SERVER_PORT'] : 80;
        $this->site = $this->protocol . $this->server . ':' . $this->port . '/' . $this->dir;

        //$this->app = $this->site.'/app';
        //$this->job = $this->site.'/job';
        $this->img = $this->site . '/img';
        $this->font = $this->site . '/fonts/lang';
    }

    public static $static_config;

    /**
     * Get Configuration Instance
     *
     * @return type
     */
    public static function getInstance()
    {
        if (!self::$static_config) {
            self::$static_config = new Config();
        }

        return self::$static_config;
    }

    /**
     * Initialize Configuration
     * Integrate YAML file into this class as properties
     *
     * @param mixed $config
     */
    public function initialize($config)
    {
        $this->config = Process::arr2obj($config);
        foreach ($this->config as $key => $value) {
            $this->$key = $value;
        }
    }


    public function isProduction()
    {
        if ((isset($this->config->app->mode)) && ($this->config->app->mode == 'production')) {
            return true;
        } else {
            return false;
        }
    }


    public static function path($path_mix)
    {
        $path_mix = str_replace('/', DIRECTORY_SEPARATOR, $path_mix);
        $path_mix = str_replace('\\', DIRECTORY_SEPARATOR, $path_mix);

        return $path_mix;
    }

    public function getImage($type, $name, $ext = '.png')
    {
        $base = '';
        eval('$base = self::img_app_' . $type . ';');
        $file_path = '/img/' . $base . '/' . $name . $ext;
        $image = $this->root . $file_path;
        if (!file_exists($image)) {
            $name = 'default';
            $file_path = '/img/' . $base . '/' . $name . $ext;
        }

        return $this->site . $file_path;
    }

    public function getSocialIconColor($icon_name)
    {
        $color = array(
            'github'   => '4c4c4c',
            'yahoo'    => '731A8B',
            'facebook' => '4a6ea9',
            'skype'    => '00AFF0',
            'twitter'  => '00ACED',
            'linkedin' => '007FB1'
        );

        return '#' . $color[$icon_name];
    }
}